package com.hluck.criminalintent.database

import android.content.Context
import androidx.room.Room
import com.hluck.criminalintent.App
import com.hluck.criminalintent.model.Crime
import java.util.*

/**
 *
 * @Author： LJH
 * @Time： 2023/11/20
 * @description：
 */
private const val DATABASE_NAME = "crime-database"
class CrimeRepository private constructor(){

    private var database: CrimeDatabase =
        Room.databaseBuilder(App.app,CrimeDatabase::class.java, DATABASE_NAME).build()
    private val crimeDao: CrimeDao = database.crimeDao()

    companion object{
        private lateinit var INSTANCE:CrimeRepository

        fun getInstance():CrimeRepository{
            if (!::INSTANCE.isInitialized){
                synchronized(CrimeRepository::class.java){
                    if (!::INSTANCE.isInitialized){
                        INSTANCE = CrimeRepository()
                    }
                }
            }
            return INSTANCE
        }
    }

    fun getCrimes():List<Crime> = crimeDao.getCrimes()
    fun getCrime(id: UUID):Crime? = crimeDao.getCrime(id)
    fun insertCrime(crime: Crime):Long = crimeDao.insertCrime(crime)
}